home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / answrbok / 7_8.lha / 7_8 / 7_8a5.h < prev    next >
Text File  |  1993-08-08  |  4KB  |  195 lines

  1. * Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
  2. * The C++ Answer Book */
  3. * Tony Hansen */
  4. * All rights reserved. */
  5. define gdlistimplement(TYPE)            \
  6.    /* clear the list */            \
  7.    void gdlist(TYPE)::clear()            \
  8.    {                        \
  9. gdlink(TYPE) *l = last;            \
  10. if (!l) return;                \
  11.                     \
  12. do  {                    \
  13.     gdlink(TYPE) *ll = l;        \
  14.     l = l->next;            \
  15.     delete ll;                \
  16. } while (l != last);            \
  17.                     \
  18. last = 0;                \
  19.    }                        \
  20.                     \
  21.    /* Get the next entry from a list, */    \
  22.    /* moving curr forward. */            \
  23.    int gdlist(TYPE)::next(TYPE &a)        \
  24.    {                        \
  25. if (!last)                \
  26.     return 0;                \
  27.                     \
  28. /* move curr forward, possibly to */    \
  29. /* the beginning or end of the list. */    \
  30. if (curr)                \
  31.     if (curr == last)            \
  32.     {                \
  33.     curr = 0;            \
  34.     return 0;            \
  35.     }                \
  36.                     \
  37.     else                \
  38.     curr = curr->next;        \
  39.                     \
  40. else                    \
  41.     curr = last->next;            \
  42.                     \
  43. a = curr->e;                \
  44. return 1;                \
  45.    }                        \
  46.                     \
  47.    /* Get the previous entry from a list, */    \
  48.    /* moving curr backwards. */        \
  49.    int gdlist(TYPE)::prev(TYPE &a)        \
  50.    {                        \
  51. if (!last)                \
  52.     return 0;                \
  53.                     \
  54. /* move curr backwards, possibly to */    \
  55. /* the beginning or end of the list. */    \
  56. if (curr)                \
  57.     if (curr == last->next)        \
  58.     {                \
  59.     curr = 0;            \
  60.     return 0;            \
  61.     }                \
  62.                     \
  63.     else                \
  64.     curr = curr->prev;        \
  65.                     \
  66. else                    \
  67.     curr = last;            \
  68.                     \
  69. a = curr->e;                \
  70. return 1;                \
  71.    }                        \
  72.                     \
  73.    /* Get the next entry from a list, */    \
  74.    /* removing it afterwards. */        \
  75.    /* curr is left alone. */            \
  76.    int gdlist(TYPE)::getnext(TYPE &a)        \
  77.    {                        \
  78. if (!last)                \
  79.     return 0;                \
  80.                     \
  81. /* choose the link to remove */        \
  82. gdlink(TYPE) *f;            \
  83. if (curr)                \
  84.     if (curr == last)            \
  85.     {                \
  86.     curr = 0;            \
  87.     return 0;            \
  88.     }                \
  89.                     \
  90.     else                \
  91.     f = curr->next;            \
  92.                     \
  93. else                    \
  94.     f = last->next;            \
  95.                     \
  96. a = f->e;                \
  97.                     \
  98. /* remove f from the list */        \
  99. if (f == last)                \
  100.     last = 0;                \
  101.                     \
  102. else                    \
  103.     f->remove();            \
  104.                     \
  105. delete f;                \
  106. return 1;                \
  107.    }                        \
  108.                     \
  109.    /* Get the previous entry from a list, */    \
  110.    /* removing it afterwards */        \
  111.    int gdlist(TYPE)::getprev(TYPE &a)        \
  112.    {                        \
  113. if (!last)                \
  114.     return 0;                \
  115.                     \
  116. /* choose the link to remove */        \
  117. gdlink(TYPE) *f;            \
  118. if (curr)                \
  119.     if (curr == last->prev)        \
  120.     {                \
  121.     curr = 0;            \
  122.     return 0;            \
  123.     }                \
  124.                     \
  125.     else                \
  126.     f = curr->prev;            \
  127.                     \
  128. else                    \
  129.     curr = last;            \
  130.                     \
  131. a = f->e;                \
  132.                     \
  133. /* remove f from the list */        \
  134. if (f == last)                \
  135.     last = 0;                \
  136.                     \
  137. else                    \
  138.     f->remove();            \
  139.                     \
  140. delete f;                \
  141. return 1;                \
  142.    }                        \
  143.                     \
  144.    /* insert an entry at the */        \
  145.    /* beginning of the list */            \
  146.    void gdlist(TYPE)::insert(TYPE a)        \
  147.    {                        \
  148. if (last)                \
  149.     last->next->            \
  150.     insert(new gdlink(TYPE)(a));    \
  151.                     \
  152. else                    \
  153.     {                    \
  154.     last = new gdlink(TYPE)(a);        \
  155.     last->next = last->prev = last;    \
  156.     }                    \
  157.    }                        \
  158.                     \
  159.    /* Append an entry at the end of the */    \
  160.    /* list. This is the same as */        \
  161.    /* gdlist(TYPE)::insert() */        \
  162.    /* except that gdlist(TYPE)::last is */    \
  163.    /* adjusted afterwards. */            \
  164.    void gdlist(TYPE)::append(TYPE a)        \
  165.    {                        \
  166. this->insert(a);            \
  167. last = last->next;            \
  168.    }                        \
  169.                     \
  170.    /* Insert an entry before the */        \
  171.    /* current location of the list. */        \
  172.    /* If curr is not set, insert at */        \
  173.    /* the beginning of the list. */        \
  174.    void gdlist(TYPE)::inserthere(TYPE a)    \
  175.    {                        \
  176. if (curr)                \
  177.     curr->insert(new gdlink(TYPE)(a));    \
  178.                     \
  179. else                    \
  180.     this->insert(a);            \
  181.    }                        \
  182.                     \
  183.    /* Append an entry after the */        \
  184.    /* current location of the list. */        \
  185.    /* If curr is not set, append at */        \
  186.    /* the end of the list. */            \
  187.    void gdlist(TYPE)::appendhere(TYPE a)    \
  188.    {                        \
  189. if (curr)                \
  190.     curr->append(new gdlink(TYPE)(a));    \
  191.                     \
  192. else                    \
  193.     this->append(a);            \
  194.    }
  195.